//
//  NoVipView.m
//  ChatForHot
//
//  Created by FLY on 2018/10/19.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "NoVipView.h"
#import "NoVipModel.h"
#import "NoVipCell.h"
#import "FLYbutton.h"
#import "PurchaseManager.h"
#import "PersonalVipInfoModel.h"
@interface NoVipView ()<UITableViewDelegate,UITableViewDataSource,ChatNoVipCellDelegate>

@end

@implementation NoVipView

- (instancetype)init{
    
    if ( self = [super init]) {
        self.backgroundColor = HOTCHATrgba(0,0,0,0.5);
        
        [self addSubview:self.HOTCHATbackView];
        [self addSubview:self.HOTCHATbuyBtn];
        [self addSubview:self.HOTCHATtopImgView];
        [self addSubview:self.tableView];
        [self.HOTCHATbackView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(380);
            make.bottom.equalTo(self.mas_bottom).offset(-38);
        }];
        [self.HOTCHATtopImgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.HOTCHATbackView.mas_top);
            make.height.mas_equalTo(233);
            make.left.equalTo(self.HOTCHATbackView.mas_left).mas_offset(15);
            make.right.equalTo(self.HOTCHATbackView.mas_right).mas_offset(-15);
        }];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.HOTCHATtopImgView.mas_bottom);
            make.height.mas_equalTo(84);
            make.left.equalTo(self.HOTCHATtopImgView.mas_left).mas_offset(2);
            make.right.equalTo(self.HOTCHATtopImgView.mas_right).mas_offset(-2);
        }];
        [self.tableView reloadData];
        [self.HOTCHATbuyBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.HOTCHATbackView.mas_left).mas_offset(20);
            make.right.equalTo(self.HOTCHATbackView.mas_right).mas_offset(-20);
            make.height.mas_equalTo(52);
            make.centerX.equalTo(self.HOTCHATbackView);
            make.bottom.equalTo(self.HOTCHATbackView.mas_bottom).offset(-8);
        }];
        [self.HOTCHATbuyBtn.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.HOTCHATbuyBtn).mas_offset(5);
            make.height.mas_equalTo(52/2-5);
        }];
        [self.HOTCHATbuyBtn.titleLab2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(52/2);
            make.left.right.equalTo(self.HOTCHATbuyBtn);
            make.height.mas_equalTo(52/2);
        }];
        
    }
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(HOTCHATtapaction:)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
    
    return self;
}
//lazy
- (UIView *)HOTCHATbackView{
    if (_HOTCHATbackView == nil) {
        _HOTCHATbackView = [[UIView alloc] init];
        _HOTCHATbackView.backgroundColor = HOTCHATrgba(255,255,255,1);
        _HOTCHATbackView.layer.cornerRadius = 20;
    }
    return _HOTCHATbackView;
}
- (UIImageView *)HOTCHATtopImgView{
    if (_HOTCHATtopImgView == nil) {
        
        _HOTCHATtopImgView = [[UIImageView alloc] init];
        _HOTCHATtopImgView.image = [UIImage imageNamed:@"novipimage"];
    }
    return _HOTCHATtopImgView;
}
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[NoVipCell class] forCellReuseIdentifier:@"NoVipCell"];
    }
    return _tableView;
}
- (void)setDataDic:(NSDictionary *)dataDic{
    
    _dataDic = dataDic;
    _dataArr = [NoVipModel HOTCHAThelpArrayWithDic:_dataDic];
    
    _HOTCHATbuyBtn.titleLab.textAlignment = NSTextAlignmentCenter;
    _HOTCHATbuyBtn.titleLab.font = [UIFont boldSystemFontOfSize:20];
    _HOTCHATbuyBtn.titleLab.text = [NSString stringWithFormat:@"%@",_dataDic[@"copy"]];
    _HOTCHATbuyBtn.titleLab.textColor = HOTCHATrgb(255,255,255);
    
    _HOTCHATbuyBtn.titleLab2.textAlignment = NSTextAlignmentCenter;
    _HOTCHATbuyBtn.titleLab2.font = [UIFont boldSystemFontOfSize:12];
    _HOTCHATbuyBtn.titleLab2.text = [NSString stringWithFormat:@"%@",_dataDic[@"price"]];
    _HOTCHATbuyBtn.titleLab2.textColor = HOTCHATrgb(255,255,255);
    if ([[NSString stringWithFormat:@"%@",_dataDic[@"price"]] length] == 0) {
        //hidden
        [self.HOTCHATbuyBtn.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.HOTCHATbuyBtn);
            make.height.mas_equalTo(52);
        }];
    }
}
- (NSArray *)dataArr{
    
    if (_dataArr == nil) {
        
        _dataArr = [[NSArray alloc]init];
    }
    
    return _dataArr;
}
- (FLYbutton *)HOTCHATbuyBtn{
    if (_HOTCHATbuyBtn == nil) {
        _HOTCHATbuyBtn = [[FLYbutton alloc] init];
        _HOTCHATbuyBtn.layer.cornerRadius = 10;
        _HOTCHATbuyBtn.clipsToBounds = YES;
        [_HOTCHATbuyBtn setBackgroundImage:[UIImage imageWithColor:HOTCHATMainColor] forState:(UIControlStateNormal)];
        [_HOTCHATbuyBtn setBackgroundImage:[UIImage imageWithColor:HOTCHATMainColor] forState:(UIControlStateHighlighted)];
        [_HOTCHATbuyBtn addTarget:self action:@selector(ugaction) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _HOTCHATbuyBtn;
}

- (void)show{
    UIView *uv = [UIApplication sharedApplication].keyWindow;
    [uv addSubview:self];
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(uv);
    }];
}

- (void)ugaction{
    [self HOTCHATbtnAction];
    //    [self removeFromSuperview];
}
- (void)HOTCHATcloseview{
    //    [self removeFromSuperview];
}
- (void)HOTCHATbtnAction{
    @weakify(self)
    PersonalVipInfoModel *model = [[PersonalVipInfoModel alloc] init];
    model.price = _dataDic[@"cost"];
    model.productid = @"VIP0201";
    model.duration = _dataDic[@"duration"];
    model.unit = _dataDic[@"unit"];
    [[PurchaseManager shareInstance] startPurchWithID:model.productid model:model completeHandle:^(HOTCHATPurchType type, NSData * _Nonnull data) {
        @strongify(self)
        [self.delegate HOTCHATbuyVipFinishFed];
        [self HOTCHATcloseview];
    }];
}
- (void)HOTCHATtapaction:(UITapGestureRecognizer *)tap{
    
    if (!_ifCanClose) {
        CGPoint point = [tap locationInView:self];
        if (CGRectContainsPoint(CGRectMake(0, 0, HOTCHATUIWIDTH, 250), point)) {
            [self HOTCHATcloseview];
        }
    }else{
        [self HOTCHATcloseview];
    }
    
}
#pragma tableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NoVipModel *entity = [self.dataArr objectAtIndex:indexPath.row];
    return entity.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NoVipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoVipCell"];
    NoVipModel *model = [self.dataArr objectAtIndex:indexPath.row];
    cell.model = model;
    cell.delegate = self;
    
    return cell;
}
- (void)HOTCHATServiceAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATServiceAction)]) {
        [self.delegate HOTCHATServiceAction];
    }
}
- (void)HOTCHATpolicyAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATpolicyAction)]) {
        [self.delegate HOTCHATpolicyAction];
    }
}
- (void)dealloc{
    //    NSLog(@"cnvtv dealloc");
}

@end
